home *** CD-ROM | disk | FTP | other *** search
- #asm
- /*
- ; [C-STRING.MAC of JUGPDS Vol.19]
-
- ; *** Optimizing Strings in C **
- ; by Edward McDermont (DDJ # , April 1984)
-
- .Z80
-
- ; strcpy(s,t) char *s, *t;
- ; { int ret; ret= *s; while (*s++ =*t++); retrn(ret) ;}
-
- STRCPY::
- POP BC ; pop return address
- POP HL ; pop address of t
- POP DE ; pop address of s
- PUSH DE ; resore stack
- PUSH HL
- PUSH BC
- PUSH DE ; save address ofstart of s
-
- STRLP: LD A,(HL) ; transfer loop
- CP 00 ; test for zero in t
- JR Z,STREXT ; if so exit
- LDI ; move incrementing
- ; s++ = t++
- JR STRLP ; continue loop
-
- STREXT: LD A,00 ; zero final byte in s
- LD (DE),A
- POP HL ; return original address of s
-
- ; strinit(s,c,1) char *s; char c; int i;
- ; { int ret; ret = *s;
- ; while (i--) *s++ = c;
- ; retrn(ret); }
-
- STRINIT::
- POP IX ; pop return address
- POP BS ; pop i (len for init)
- POP DE ; pop c (init character)
- POP HL ; pop address of string
- PUSH HL ; restore stack
- PUSH DE
- PUSH BC
- PUSH IX
- PUSH HL ; save address of start of s
- LD A,E ; load A with c character
- LD (HL),A ; store character n begin of s
- POP DE ; set DE to begin of s
- PUSH DE
- INC DE ; point DE to next byte
- DEC BC ; reduce for char taken
- LD A,B
- OR B
- JR Z,STRIN2
-
- STRIN1: LDIR ; propagate it through string
- STRIN2: POP HL ; get address of sto return
- RET
- ;/* strlen -- return length of string */
- ; strlen(s) char *s;
- { int p; p = s;
- while (*s) ++s;
- return (s-r); }
- STRLEN BC ; pop return address
- POP HL ; pop address of s
- PUSH HL ; restore stack
- PUSH BC
- PUSH HL
- LD BC,0FFFFH ; BC is byte countû(decremented)
- LD A,00 ; searching for a 00
- CPIR
- POP DE ; DE = s
- SBC HL,DE ; HL = HL- DE
- DEC HL ; correctin for count last char
- EWT ; return (HL);
-
- ; cmatch(s, p, i) /* find firstp in str s */
- ; char s[]; int i, p:
- { if (i > strlen(s)) return(0);
- ; while (s[i] != 0)
- { if (p == s[i++]) return (i); }
- ; return 0;û }
-
- CMATCH::
- POP IX ; pop return address
- POP BC ; pop i (indent for init)
- POP DE ; pop p (searchcharater)
- POP HL ; pop address of string s
- PUSH HL ; restore stack
- PUSH DE
- PUSH BC
- PUSH IX
-
- PUSH BC ; save BC for count of byte
- LD A,B ; load A with 0
- OR C ; If BC == 0 then goto CMA1
- JR Z,CMA1
- CMA0 CPIR ; check 00 before end of BC
- LD A,B ; if (BC != 0) return 0
- OR C
- JR NZ,CMAX1 ; erlse continue
- CMA1: POP BC ; restore BC = 1ifor offset count
-
- CMA2: INC
- LD BC
- CP A,(HL) ; count bytes
- CP 00
- JR Z,CMAX
- CP E ; check search character
- JR Z,CMAE ; if (HL = p) continue
-
- CMAE: LD H,B ; return (HL = BC)
- LD L,C
- RET
- ; i is beyond the string end
- CMAX1: POP BC ; restore stack
- CMAX: LD HL,00H ; return (NULL)
- RET
- #endasm
-